package com.partners.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.partners.constant.ErrorMessageEnum;
import com.partners.constant.PermissionEnum;
import com.partners.dao.basic.UserDao;
import com.partners.dao.basic.UserRoleDao;
import com.partners.dao.bo.UserDO;
import com.partners.dao.bo.UserRoleDO;
import com.partners.dao.dto.PermissionOutputDTO;
import com.partners.dao.dto.RolePermissionDTO;
import com.partners.dao.basic.PermissionDao;
import com.partners.dao.basic.RolePermissionDao;
import com.partners.dao.bo.PermissionDO;
import com.partners.dao.bo.RolePermissionDO;
import com.partners.model.BusinessException;
import com.partners.model.Paging;
import com.partners.service.PermissionService;
import com.partners.service.UserService;
import com.partners.utils.TimestampUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liangyongle
 * @date 2018-12-08
 */
@Service
@Slf4j
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private PermissionDao permissionDao;

    @Resource
    private UserService userService;

    @Resource
    private RolePermissionDao rolePermissionDao;

    @Resource
    private UserDao userDao;


    @Resource
    private UserRoleDao userRoleDao;


    /**
     * 获取我的权限列表
     * @param  uid Integer
     * @return Object
     */
    @Override
    public List<PermissionOutputDTO> getMyPermissionList(Integer uid) {
        //1.先查询此用户是否存在
        UserDO userDO = userDao.selectByPrimaryKey(uid);
        if(userDO == null){
            throw BusinessException.of(ErrorMessageEnum.USER_NOT_EXIST);
        }
        //2.查询用户角色
        UserRoleDO userRoleDO = userRoleDao.selectByPrimaryKey(uid);
         if(userRoleDO != null){
             //3.查询角色的权限列表
             List<PermissionOutputDTO> list = permissionDao.getPermissionListByRoleId(userRoleDO.getRoleId());
             return list;
         }else{
             return null;
         }



    }



    /**
     * 获取权限列表
     * @param  paging Paging
     * @return Object
     */
    @Override
    public Map<String, Object> getPermissionList(Paging paging,Integer uid) {
        //检查权限
        Integer code = PermissionEnum.PERMISSION_LIST.getCode();
        userService.isPermission(uid, code);
        PageHelper.startPage(paging.getPage(),paging.getRows());
        Map<String,Object> allList = new HashMap<>();
        List<Map<String,Object>> list = permissionDao.getPermissionList();
        Page page = (Page) list;
        Object total = page.getTotal();
        allList.put("total",total);
        allList.put("list",list);
        return allList;
    }

    /**
     * 获取权限详情
     * @param id Integer
     * @return Object
     */
    @Override
    public PermissionDO getPermissionDetail(Integer id) {
        PermissionDO permission = permissionDao.selectByPrimaryKey(id);
        if(permission == null){
            throw BusinessException.of(ErrorMessageEnum.RECORD_NOT_FOUND);
        }
        return permission;
    }


    /**
     * 编辑权限
     * @param permissionDO PermissionDO
     * @return Object
     */
    @Override
    public Integer editPermission(PermissionDO permissionDO) {
        Integer result = 0;
        Integer exist = permissionDao.getPermissionByName(permissionDO.getName(),permissionDO.getId());
        if(exist != null){
            throw BusinessException.of(ErrorMessageEnum.NAME_IS_EXIST);
        }
        if(permissionDO.getId() != null){
            //修改
            result = permissionDao.updateByPrimaryKeySelective(permissionDO);
        }else{
            //新增
            //查询最大的编号
            Integer lastCode = permissionDao.getLastPermissionCode();
            permissionDO.setAddtime(TimestampUtils.timestamp());
            permissionDO.setCode(lastCode+1);
            result = permissionDao.insert(permissionDO);
        }
        return result;
    }

    /**
     * 获取角色权限列表
     * @param paging Paging roleId Integer
     * @return Object
     */
    @Override
    public Map<String, Object> getRolePermission(Paging paging, Integer roleId) {
        PageHelper.startPage(paging.getPage(),paging.getRows());
        Map<String,Object> allList = new HashMap<>();
        List<Integer> list = permissionDao.getRolePermission(roleId);
        List<PermissionDO> permission = permissionDao.getRolePermissions(list);
        PageInfo pageInfo = new PageInfo(permission);
        Object total = pageInfo.getTotal();
        allList.put("total",total);
        allList.put("list",permission);
        return allList;
    }

    /**
     * 编辑权限
     * @param rolePermissionDO RolePermissionDO
     * @return boolean
     */
    @Override
    public Integer editRolePermission(RolePermissionDO rolePermissionDO) {
        Integer result = 0;
        Object exist = permissionDao.getExistRolePermission(rolePermissionDO.getRoleId(),rolePermissionDO.getPid());
        if(exist != null){
            throw BusinessException.of(ErrorMessageEnum.RECORD_IS_EXIST);
        }else{
            rolePermissionDO.setAddtime(TimestampUtils.timestamp());
            result = rolePermissionDao.insert(rolePermissionDO);
        }
        return result;
    }


    /**
     * 批量编辑权限
     * @param rolePermissionDTO RolePermissionDTO
     * @return Object
     */
    @Override
    @Transactional
    public Boolean editPerRole(RolePermissionDTO rolePermissionDTO) {
        List<RolePermissionDTO.RolePermission> list = rolePermissionDTO.getList();
        //遍历
        for (RolePermissionDTO.RolePermission map : rolePermissionDTO.getList()) {
            RolePermissionDO rolePermission = new RolePermissionDO();
            Integer pid = map.getPid();
            Integer roleId = map.getRoleId();
            Object exist = permissionDao.getExistRolePermission(roleId,pid);
            if(exist == null){
                rolePermission.setPid(pid);
                rolePermission.setRoleId(roleId);
                rolePermission.setAddtime(TimestampUtils.timestamp());
                rolePermissionDao.insert(rolePermission);
            }
        }
        return true;
    }

    /**
     * 删除权限
     * @param id Integer
     * @return boolean
     */
    @Override
    public Integer deletePermission(Integer id) {
        //查询是否存在记录
        PermissionDO permission = permissionDao.selectByPrimaryKey(id);
        if(permission == null){
            throw  BusinessException.of(ErrorMessageEnum.RECORD_NOT_FOUND);
        }
        Integer result = permissionDao.deleteByPrimaryKey(id);
        return result;
    }

    /**
     * 删除权限关系
     * @param roleId Integer pid Integer
     * @return boolean
     */
    @Override
    public Integer deleteRolePermission(Integer roleId, Integer pid) {
        Integer result = permissionDao.deleteRolePermission(roleId,pid);
        return result;
    }



}
